/*
 *******************************************************************************
 *
 * Copyright (C) 2019-2020 Dialog Semiconductor.
 * This computer program includes Confidential, Proprietary Information
 * of Dialog Semiconductor. All Rights Reserved.
 *
 *******************************************************************************
 */

package com.dialog.suotalib.suota;

public class HeaderInfo58x extends HeaderInfo {

    /*
    typedef struct
    {
        uint8_t signature[2];
        uint8_t validflag;      // Set to STATUS_VALID_IMAGE at the end of the image update
        uint8_t imageid;        // it is used to determine which image is the newest
        uint32_t code_size;     // Image size
        uint32_t CRC ;          // Image CRC
        uint8_t version[16];    // Vertion of the image
        uint32_t timestamp;
        uint8_t encryption;
        uint8_t reserved[31];
    } image_header_t;

    #define IMAGE_HEADER_SIGNATURE1     0x70
    #define IMAGE_HEADER_SIGNATURE2     0x51
    */

    public static final String TYPE = "58x";
    public static final int SIGNATURE = 0x7051;
    public static final int HEADER_SIZE = 64;

    private static final int OFFSET_VALID_FLAG = 2;
    private static final int OFFSET_IMAGE_ID = 3;
    private static final int OFFSET_PAYLOAD_SIZE = 4;
    private static final int OFFSET_PAYLOAD_CRC = 8;
    private static final int OFFSET_VERSION = 12;
    private static final int VERSION_LENGTH = 16;
    private static final int OFFSET_TIMESTAMP = 28;
    private static final int OFFSET_ENCRYPTION = 32;
    private static final int OFFSET_PAYLOAD = 64;

    private short validFlag;
    private short imageId;
    private short encryption;

    public HeaderInfo58x(byte[] header, long totalBytes) {
        super(header, totalBytes);
    }

    public HeaderInfo58x(byte[] raw) {
        super(raw);
    }

    @Override
    protected void initializeTypeSpecific() {
        validFlag = (short) (buffer.get(OFFSET_VALID_FLAG) & 0xff);
        imageId = (short) (buffer.get(OFFSET_IMAGE_ID) & 0xff);
        encryption = (short) (buffer.get(OFFSET_ENCRYPTION) & 0xff);
    }

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public int getSignature() {
        return SIGNATURE;
    }

    @Override
    public int getHeaderSize() {
        return HEADER_SIZE;
    }

    @Override
    protected int getOffsetPayloadSize() {
        return OFFSET_PAYLOAD_SIZE;
    }

    @Override
    protected int getOffsetPayloadCrc() {
        return OFFSET_PAYLOAD_CRC;
    }

    @Override
    protected int getOffsetVersion() {
        return OFFSET_VERSION;
    }

    @Override
    protected int getVersionLength() {
        return VERSION_LENGTH;
    }

    @Override
    protected int getOffsetTimestamp() {
        return OFFSET_TIMESTAMP;
    }

    @Override
    public long getPayloadOffset() {
        return OFFSET_PAYLOAD;
    }

    public short getValidFlag() {
        return validFlag;
    }

    public short getImageId() {
        return imageId;
    }

    public short getEncryption() {
        return encryption;
    }
}
